package com.vcredit.bird.service.accumulation;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.api.IAccumulationService;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.bird.vo.accumulation.AccDetail;
import com.vcredit.bird.vo.accumulation.AccInit;
import com.vcredit.bird.vo.accumulation.AccParentParam;
import com.vcredit.bird.vo.accumulation.AccProvinceParam;
import com.vcredit.creditcat.exception.BirdRequestFailException;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.BaseException;
import com.vcredit.framework.util.BeanUtil;
import com.vcredit.framework.util.JacksonUtil;

@Service(value = "accumulationServiceImpl")
public class AccumulationService extends BirdService implements IAccumulationService {

	@Value("${gjj.support.province}")
	private String SUPPORT_URL;
	@Value("${gjj.query.form}")
	private String QUERY_URL;
	@Value("${gjj.init.url}")
	private String INIT_URL;
	@Value("${gjj.login.url}")
	private String LOGIN_URL;
	@Value("${gjj.query.url}")
	private String QUERYDETAIL_URL;

	@Log
	@Override
	public List<AccProvinceParam> accGetSupportCity() throws Exception {
		BirdResponse acc = super.getBirdData(SUPPORT_URL);
		return JacksonUtil.Json2Obj(acc.getResult(), new TypeReference<List<AccProvinceParam>>() {
		});
	}

	@Log
	@Override
	public String accQueryFormByCity(String city) throws Exception {
		BirdResponse result = super.getBirdData(QUERY_URL + "/" + city + "/Json");
		return result.getResult();
	}

	@Log
	@Override
	public AccInit accInit(String city) throws Exception {
		BirdResponse result = super.getBirdData(INIT_URL + "/" + city + "/Json");
		AccInit init = new AccInit();
		return BeanUtil.copy(result, init);
	}

	@Override
	@Log
	public AccParentParam accLogin(String city, Map<String, Object> param) throws Exception {
		BirdResponse response = super.postBirdData(LOGIN_URL + "/" + city + "/Json", JacksonUtil.Obj2Json(param), 120);
		AccParentParam result = new AccParentParam();
		BeanUtil.copy(response, result);
		result.setStatusCode(response.getCode().toString());
		result.setStatusDescription((response.getDesc()));
		return result;
	}

	@Log
	@Override
	public AccDetail accQuery(String city, Map<String, Object> param) throws BirdRequestFailException,BaseException, Exception {
		BirdResponse response = super.postBirdData(QUERYDETAIL_URL, JacksonUtil.Obj2Json(param));
		return JacksonUtil.Json2Obj(response.getResult(), new TypeReference<AccDetail>() {
		});
	}

}
